package com.myhome.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.domain.SysUserRole;
import com.myhome.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myhome.mapper.SysUserMapper;
import com.myhome.domain.SysUser;
import com.myhome.service.SysUserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
*@Author QYiXin 
* @Date 2021 10 09 12 58
**/
@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{
    @Autowired
    SysUserRoleService sysUserRoleService;
    @Autowired
    SysUserMapper sysUserMapper;
    @Override
    public Page<SysUser> getPageUsers(Page<SysUser> page, String mobile, String fullName) {
        Page<SysUser> result = this.page(page, new LambdaQueryWrapper<SysUser>().like(
                !StringUtils.isEmpty(mobile),
                SysUser::getMobile,
                mobile
        ).like(
                !StringUtils.isEmpty(fullName),
                SysUser::getFullname,
                fullName
        ));
        List<SysUser> records = result.getRecords();
        if (!CollectionUtil.isEmpty(records)) {
            for (SysUser record : records) {
                List<SysUserRole> list = sysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, record.getId()));
                if(!CollectionUtils.isEmpty(list)){
                    record.setRole_strings(
                            list.stream().
                                    map(sysUserRole -> sysUserRole.getRoleId().toString())
                                    .collect(Collectors.joining(",")));
                }
            }
        }
        return result;
    }

    @Override
    public Boolean addUser(SysUser sysUser) {
        sysUser.setPassword(new BCryptPasswordEncoder().encode(sysUser.getPassword()));
        String roleStr = sysUser.getRole_strings();
        boolean a = false;
        boolean b = false;
        b = this.save(sysUser);
        if (!StringUtils.isEmpty(roleStr)) {
            String[] roleIds = roleStr.split(",");
            List<SysUserRole> sysUserRoles = new ArrayList<>(roleIds.length);
            for (String roleId : roleIds) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setRoleId(Long.valueOf(roleId));
                Long id = sysUser.getId();
                sysUserRole.setUserId(sysUser.getId());
                sysUserRoles.add(sysUserRole);
            }
            a = sysUserRoleService.saveBatch(sysUserRoles);
        }
        return a && b;

    }

    @Override
    public Boolean removeUserWithRoles(Long[] ids) {
        boolean a = this.removeByIds(Arrays.asList(ids));
        for (Long id : ids) {
            sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, id));
        }
        return a;
    }

    @Override
    public Boolean updateUser(SysUser sysUser) {
        this.updateById(sysUser);
        sysUserRoleService.remove(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, sysUser.getId()));
        String role_strings = sysUser.getRole_strings();
        String[] roles = role_strings.split(",");
        List<SysUserRole> sysUserRoleList = new ArrayList<>(roles.length);
        for (String roleStr : roles) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getId());
            sysUserRole.setRoleId(Long.valueOf(roleStr));
            sysUserRoleList.add(sysUserRole);
        }
        Boolean isUpdate = sysUserRoleService.saveBatch(sysUserRoleList);
        return isUpdate;
    }


}
